package com.dwjm.exam.admin;

import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/admin/add")
public class AdminAddHandler extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");

        PrintWriter out = resp.getWriter();
        Map<String, Object> responseMap = new HashMap<>();
        
        try {
            // 获取表单参数
            String username = req.getParameter("username");
            String password = req.getParameter("password");
            String realName = req.getParameter("realName");
            String email = req.getParameter("email");
            String phone = req.getParameter("phone");
            String role = req.getParameter("role");
            String statusStr = req.getParameter("status");
            
            // 验证必填参数
            if (username == null || password == null || 
                username.trim().isEmpty() || password.trim().isEmpty()) {
                responseMap.put("code", 400);
                responseMap.put("msg", "用户名和密码为必填项");
                out.write(new Gson().toJson(responseMap));
                return;
            }
            
            // 创建Admin对象
            Admin admin = new Admin();
            admin.setUsername(username);
            admin.setPassword(password);
            admin.setRealName(realName);
            admin.setEmail(email);
            admin.setPhone(phone);
            admin.setRole(role != null && !role.isEmpty() ? role : "admin");
            admin.setStatus(statusStr != null && !statusStr.isEmpty() ? Integer.parseInt(statusStr) : 1);
            
            // 调用DAO添加管理员
            AdminDaoImpl adminDao = new AdminDaoImpl();
            int result = adminDao.addAdmin(admin);
            
            if (result > 0) {
                responseMap.put("code", 200);
                responseMap.put("msg", "添加管理员成功");
                responseMap.put("data", result); // 返回生成的ID
            } else if (result == -1) {
                responseMap.put("code", 400);
                responseMap.put("msg", "用户名已存在");
            } else {
                responseMap.put("code", 500);
                responseMap.put("msg", "添加管理员失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            responseMap.put("code", 500);
            responseMap.put("msg", "服务器错误: " + e.getMessage());
        }
        
        // 输出响应
        Gson gson = new Gson();
        out.write(gson.toJson(responseMap));
        out.flush();
        out.close();
    }
} 